<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 今日菜谱
 *
 * @author www.dingwei.cn
 */

class AdmDailyMenusModel extends MY_Model{
	
	private $tbDailyMenus = 'admin_daily_menus';
	
	/**
	 * 保存
	 * @param 	array 		$data
	 * @param 	integer 	$id
	 * @return	array
	 */
	public function save($data, $id = 0){
		if($id){
			$this->db->where('id', $id);
			$tA = $this->update($this->tbDailyMenus, $data);
		}else{
            $tA = $this->insert($this->tbDailyMenus, $data);
		}
		
		return $tA;
	}

    /**
     * 历史反馈意见
     * @param $consumerId  消费者id
     * @param $merchantId  商户id
     * @param $page
     * @return array
     * @khj
     */
    public function appGetList($page){

        $this->db->select('id,pic,title,desc,addtime')
            ->order_by('is_recommend desc,addtime desc');

        $tA = $this->appPage($this->tbDailyMenus,$page,true);

        foreach ($tA['list'] as $key=>&$value){
            $value['addtime'] = dateFormat($value['addtime'],true);
            $value['pic'] = $value['pic']?getImage($value['pic'],'og'):'';
        }

        return $tA;
    }

    /**
     * 获取详情
     * @param $id
     * @return array
     */
    public function getInfo($id){
        if (!$id){
            return array();
        }
        $this->db->select('id,title,pic,production_time,desc,content,is_recommend');
        $this->db->where(array('id' => $id));
        $tA = $this->getOne($this->tbDailyMenus);
        if (!empty($tA)){
            $tA['pic_og'] = $tA['pic'];
            $tA['pic'] = base_url($tA['pic']);
            $tA['content'] = str_replace('{base_url}', base_url(), $tA['content']);
        }
        return $tA;
    }

    /**
     * 后台获取列表
     * @return  array
     */
	public function adminPcGetList(){
	    $recommend = (int)get('is_recommend');
        $send_date_from = trim(get('send_date_from'));
        $send_date_to = trim(get('send_date_to'));
        $iSortCol_0 = (int)get('iSortCol_0');//针对某一列进行排序
        $sSortDir_0 = trim(get('sSortDir_0'));//排序方式，值为"asc"或者"desc"

        if ($recommend){
	        $this->db->where(array('is_recommend' => $recommend));
        }
        if (strlen($send_date_from) && strlen($send_date_to)){
            $this->db->where(array('addtime >= ' => strtotime($send_date_from), 'addtime <= ' => strtotime($send_date_to)));
        }

        $this->db->select('id,pic,title,is_recommend,addtime');
        if ($iSortCol_0 == 3){
            $this->db->order_by('addtime '.$sSortDir_0);
        }else{
            $this->db->order_by('id desc');
        }
        $data = $this->pagination($this->tbDailyMenus);
        return $data;
    }

    /**
     * 删除
     * @param int       $id
     * @return array|bool
     */
    public function deleteDailyMenus($id){
        if (!$id) return array();
        $this->db->where(array('id' => $id));
        return $this->delete($this->tbDailyMenus );
    }
}